package com.nbcb.nwql.applicantion.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.nbcb.nwql.applicantion.domain.UserLoginInfo;
import com.nbcb.nwql.applicantion.repository.*;
import com.nbcb.nwql.common.rtnvo.ApiResponse;
import com.nbcb.nwql.glowbal.annotation.RecordLogsAspectAt;
import lombok.extern.slf4j.Slf4j;
import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.exception.MemcachedException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeoutException;

@RestController
@RequestMapping("pc")
@Slf4j
public class LoginApi {
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    private MemcachedClient memcachedClient;
    @Autowired
    private MenuInfoMapper menuInfoMapper;
    @Autowired
    private MenuRoleInfoMapper menuRoleInfoMapper;
    @Autowired
    private RoleInfoMapper roleInfoMapper;
    @Autowired
    private UserLoginInfoMapper userLoginInfoMapper;
    @Autowired
    private UserRoleInfoMapper userRoleInfoMapper;

    /***
     * 登录
     * @param userLoginRq 请求参数
     * @return ApiResponse
     * @throws InterruptedException 异常
     * @throws MemcachedException 异常
     * @throws TimeoutException 异常
     */
    @PostMapping("/login")
    @RecordLogsAspectAt
    public ApiResponse login(@RequestBody Map<String, Object> userLoginRq, HttpServletRequest request) throws InterruptedException, MemcachedException, TimeoutException {
        //先去查询缓存信息，从redis查询登录人账号密码，如果redis没有，则去数据库进行数据校验，并进行mq汇源
        HttpSession session = request.getSession();

        String orLogId = String.valueOf(userLoginRq.get("logId"));
        String orPassword = String.valueOf(userLoginRq.get("password"));
        
//        String redisKey = CommonUtil1.redisKeySuff + orLogId;
//        String xmemCachedKey = CommonUtil1.xmemcachedKeySuff + orLogId;
//        Object redisLoginInfo =
//                redisTemplate.opsForValue().get(redisKey);
//        Object xmemcachedLoginInfo =
//                memcachedClient.get(xmemCachedKey);

        //根据账号查询密码    从redis查询，如果相同，则直接登录，不同，查询数据库
        QueryWrapper<UserLoginInfo> query = new QueryWrapper<>();
        query.select("password", "login_id", "cust_nm", "cust_id", "remark");
        query.eq("login_id", orLogId);
        List<UserLoginInfo> userLoginInfos = userLoginInfoMapper.selectList(query);


        if (CollectionUtils.isEmpty(userLoginInfos) || userLoginInfos.size() > 1) {
            return ApiResponse.erro(201, "用户信息异常，请联系管理员", null);
        }
        String password = userLoginInfos.get(0).getPassword();
        if (orPassword.equals(password)) {//表示可以登录,密码不可以存到缓存，直接去验证
            //相同，直接登录
            //异步请求，请求mq去把当前的session信息，放进redis

            return ApiResponse.success(200, "登录成功");
        } else {//不同，应该登录失败
            return ApiResponse.erro(201, "账号或者密码错误", null);
        }
//        if (StringUtils.isEmpty(redisLoginInfo)) {
//            redis为空，则去查数据库，并且进行redis和xmemchached数据回源
//
//            if (StringUtils.isEmpty(xmemcachedLoginInfo)) {//redis、xmemcached都为空，全部回源
//                //数据库验证+xmemcached回源+redis数据回源
//                //回源
//                memcachedClient.set(xmemCachedKey, 0, orLogId);
//                redisTemplate.opsForValue().set(xmemCachedKey, password, 0);
//                return ApiResponse.ok(CommonUtil1.success);
//            } else {//redis为空，xmemcached不为空，直接进行校验登录
//                //数据库验证+redis数据回源,数据库验证+xmemcached回源+redis数据回源
//                if (!password.equals(String.valueOf(orPassword))) {//密码不一致要继续回源
//                    memcachedClient.set(xmemCachedKey, 0, orLogId);
//                }
//                redisTemplate.opsForValue().set(redisKey, password, 0);
//                return ApiResponse.ok(CommonUtil1.success);
//            }
//        } else {
//            //redis不为空，xmemchached 如果为空，则把密码回源到xmemchached, 如果xmemchached不为空，则直接校验
//            if (StringUtils.isEmpty(xmemcachedLoginInfo)) {//redis不为空、xmemcached为空，回源xmemcached
//                //数据库验证+xmemcached回源
//                memcachedClient.set(xmemCachedKey, 0, orLogId);
//                return ApiResponse.ok(CommonUtil1.success);
//            } else {//redis不为空，xmemcached不为空，直接进行校验登录,xmemcached验证，不进行回源
//                if (!password.equals(String.valueOf(orPassword))) {//密码不一致要继续回源
//                    memcachedClient.set(xmemCachedKey, 0, orLogId);
//                }
//                redisTemplate.opsForValue().set(redisKey, password, 0);
//                return ApiResponse.ok(CommonUtil1.success);
//            }
//
//        }
    }
}
